<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: product_ask.php 135 2013-09-22 07:30:55Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 135 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:30:55 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

$module_smarty = new Smarty();

if(isset($_POST['action']) && $_POST['action'] == 'ask') {
	$error = false;
	
	if(($_POST['security_code_ask'] != $_SESSION['security_code_ask']) || (!empty($_SESSION['security_code_ask']) && empty($_POST['security_code_ask'])))
		$error = true;
	
	if(empty($_POST['name_user']) || empty($_POST['email_user']) || empty($_POST['user_question']))
		$error = true;
	
	if(isset($_POST['callback']) && $_POST['callback'] == 'yes') {
		if(empty($_POST['telephone']) && 
			!preg_match('/^(?:0{2}|\+)[0-9]{2,3} [0-9]+ [0-9]+$/', $_POST['telephone']) && 
			empty($_POST['callback_date']))
			$error = true;	
	}
	
	if(!$error) {		
		$smarty->assign('P_NAME', '<a href="'.href_link(FILENAME_PRODUCT_INFO, 'products_id='.$_POST['products_id']).'">'.$product->data['products_name'].'</a>');
		$smarty->assign('USER_NAME', $_POST['name_user']);
		$smarty->assign('USER_EMAIL', $_POST['email_user']);
		$smarty->assign('ASK_TO_PRODUCT_MESSAGE', nl2br($_POST['user_question']));
		if(isset($_POST['callback']) && $_POST['callback'] == 'yes') {
			$smarty->assign('CALLBACK_TELEPHONE', $_POST['telephone']);
			$smarty->assign('CALLBACK_DATE', $_POST['callback_date']);
			$smarty->assign('CALLBACK_TIME', $_POST['callback_time']);
		}

		$smarty->caching = false;
		require_once(DIR_FS_INC.'inc.get_mail_body.php');
		$html_mail  = $smarty->fetch('html:ask_to_product');
		$html_mail .= $signatur_html;
		$txt_mail  = $smarty->fetch('txt:ask_to_product');
		$txt_mail .= $signatur_text;
		require_once(DIR_FS_INC.'inc.get_mail_data.php');
		$mail_data = get_mail_data('ask_to_product');
		
		$ask_subject = str_replace('{#customers_name#}', $_POST['name_user'], $mail_data['EMAIL_SUBJECT']);
		
		php_mail($_POST['email_user'],
				$_POST['name_user'],
				STORE_OWNER_EMAIL_ADDRESS,
				STORE_OWNER,
				'',
				$_POST['email_user'],
				$_POST['name_user'],
				'',
				'',
				$ask_subject,
				$html_mail,
				$txt_mail);

		$message_stack->add_session(TEXT_EMAIL_SEND, 'success');
		redirect(href_link(FILENAME_PRODUCT_INFO, 'products_id='.$_POST['products_id']).'#ask');

	} else
		$message_stack->add(TEXT_EMAIL_SEND_ERROR, 'error');
}

$module_smarty->assign('ASK_FORM', draw_form('product_ask', href_link(FILENAME_PRODUCT_INFO, 'products_id='.$product->data['products_id']).'#ask').draw_hidden_field('action', 'ask').draw_hidden_field('products_id', $product->data['products_id']));
$module_smarty->assign('PRODUCTS_NAME', $product->data['products_name']);

if(isset($_SESSION['customer_id'])) {
	$get_data = $db->db_query("SELECT 
									customers_email_address 
								FROM 
									".TABLE_CUSTOMERS." 
								WHERE 
									customers_id = '".$_SESSION['customer_id']."'");
	
	$module_smarty->assign('NAME_USER', draw_input_field('name_user', (isset($_POST['name_user']) ? $_POST['name_user'] : $_SESSION['customer_first_name'].' '.$_SESSION['customer_last_name'])));
	$module_smarty->assign('EMAIL_USER', draw_input_field('email_user', (isset($_POST['email_user']) ? $_POST['email_user'] : $get_data->fields['customers_email_address'])));
} else {
	$module_smarty->assign('NAME_USER', draw_input_field('name_user', $_POST['name_user']));
	$module_smarty->assign('EMAIL_USER', draw_input_field('email_user', $_POST['email_user']));
}

$module_smarty->assign('TEXT_AREA', draw_textarea_field('user_question', '', 1, 1, (isset($_POST['user_question'])?$_POST['user_question']:''), 'class="w96p"'));

$module_smarty->assign('INPUT_CHECK_CALLBACK', draw_checkbox_field('callback', 'yes', (isset($_POST['callback'])?true:false), 'class="callback_open"'));
$module_smarty->assign('TELEPHONE', draw_input_field('telephone', $_POST['telephone']));
$module_smarty->assign('CALLBACK_DATE', draw_input_field('callback_date', $_POST['callback_date'], 'class="callback_date w85"'));

$captcha_site = '_ask';
include('captcha.php');
$module_smarty->assign('VVIMG', '<img width="200" height="70" src="captcha.php?show=true&name=ask" alt="Captcha" /> ');
$module_smarty->assign('INPUT_CAPTCHA', draw_input_field('security_code'.$captcha_site, '', 'size="6" maxlength="6" style="width:25px; text-align:center" id="security_code"', 'text', false));

for($i=0;$i<24;$i++){
	$time_arr[] = array('id'=>$i.':00','text'=>$i.':00');
	$time_arr[] = array('id'=>$i.':30','text'=>$i.':30');
}

function minutes_round(){
    $seconds = strtotime(date('H:i:s'));
    $rounded = ceil($seconds / (30 * 60)) * (30 * 60);
    return date('H:i', $rounded);
}

$module_smarty->assign('CALLBACK_TIME', draw_pull_down_menu('callback_time', $time_arr, (!isset($_POST['callback_time'])?minutes_round():$_POST['callback_time']), 'class="w85"'));
if(!isset($_POST['call_back']) || !$POST)
	$module_smarty->assign('HIDDEN', ' dn');

$module_smarty->assign('BUTTON_SEND', image_submit('', IMAGE_BUTTON_SEND));
$module_smarty->assign('language', $_SESSION['language']);

$module_smarty->caching = false;
$product_ask = $module_smarty->fetch(CURRENT_TEMPLATE.'/module/products_ask.html');

$info_smarty->assign('MODUL_ASK', $product_ask);
?>